//
//  ZDTabBarController.m
//  界面
//
//  Created by 跑腿仔 on 15/12/11.
//  Copyright © 2015年 paotuizai. All rights reserved.
//

#import "ZDTabBarController.h"
#import "ZTTabBar.h"

@interface ZDTabBarController () <ZTTabBarDelegate>

@end

@implementation ZDTabBarController

- (void)viewDidLoad {
    
    [super viewDidLoad];
 
    // 添加子控制器
    [self addChildVc:[[MainViewController alloc] init] title:@"首页" image:@"1" selectedImage:@"11"];
    [self addChildVc:[[CommunityViewController alloc] init] title:@"社区" image:@"2" selectedImage:@"22"];
    [self addChildVc:[[PropertyViewController alloc] init] title:@"物业" image:@"3" selectedImage:@"33"];
    [self addChildVc:[[MyViewController alloc] init] title:@"我的" image:@"4" selectedImage:@"44"];
    ZTTabBar *tabBar = [[ZTTabBar alloc] init];
    tabBar.delegate = self;
    // KVC：如果要修系统的某些属性，但被设为readOnly，就是用KVC，即setValue：forKey：。
    [self setValue:tabBar forKey:@"tabBar"];
}

/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字(可以设置tabBar和navigationBar的文字)
    childVc.title = title;

    // 设置子控制器的tabBarItem图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    // 禁用图片渲染
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    // 设置文字的样式
    //[childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : RGBColor(123, 123, 123)} forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:0.400 green:0.000 blue:0.000 alpha:1.000]} forState:UIControlStateSelected];
    //    childVc.view.backgroundColor = RandomColor; // 这句代码会自动加载主页，消息，发现，我四个控制器的view，但是view要在我们用的时候去提前加载

    // 为子控制器包装导航控制器
//    ZTNavigationController *navigationVc = [[ZTNavigationController alloc] initWithRootViewController:childVc];
    // 添加子控制器
    [self addChildViewController:childVc];
}

#pragma ZTTabBarDelegate
/**
 *  加号按钮点击
 */
- (void)tabBarDidClickPlusButton:(ZTTabBar *)tabBar
{
//    UIViewController *vc = [[UIViewController alloc] init];
//    [self presentViewController:vc animated:YES completion:nil];
}

//- (void)handleButton
//{
//    self.tabBarController.tabBar.translucent = NO;
//    NSMutableArray *titleArr = @[@"首页", @"社区", @"物业", @"我的"].mutableCopy;
//    NSMutableArray *imaArr = @[@"1", @"2", @"3", @"4"].mutableCopy;
//    NSMutableArray *selectArr = @[@"11", @"22", @"33", @"44"].mutableCopy;
//    NSMutableArray * btnsArray = [NSMutableArray array];
//    for (int i=0; i<imaArr.count; i++) {
//        ZDTabBarButton * btn = [ZDTabBarButton tabBarButtonWithTitle:titleArr[i] image:imaArr[i] selectedImage:selectArr[i]];
//        
//        [btnsArray addObject:btn];
//    }
//    //bigBtn的各种设置（可以说成是bigButton的创建）
//    UIButton * bigBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.bigBtn = bigBtn;
//    //[bigBtn addTarget:self action:@selector(openDoorBtn) forControlEvents:UIControlEventTouchUpInside];
//    [bigBtn setBackgroundImage:[UIImage imageNamed:@"未标题-1"] forState:UIControlStateNormal];
//    CGFloat W, H, w, h;
//    W = Main_Screen_Width;
//    H = Main_Screen_Height;
//    w = W * 0.195;
//    h = W * 0.22;
//    bigBtn.frame = CGRectMake(0, 0, 44 * 0.75, 55 * 0.75);
//    bigBtn.center = CGPointMake(Main_Screen_Width / 2, 26);
//    
//    ZDTabBarView * myTabBar = [ZDTabBarView tabBarViewWithButtons:btnsArray];
//    _myTabBar = myTabBar;
//    [self.tabBar addSubview:_myTabBar];
//    
//    _myTabBar.frame = self.tabBar.bounds;
//    
//    _myTabBar.delegate = self;
//    
//    [self.tabBar addSubview:bigBtn];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

////点击BigButton弹出来的界面的设置
//- (void)openDoorBtn{
//    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
//    _openDoorArray = [user objectForKey:@"community"];
//    _userId = [user objectForKey:@"userId"];
//    if (_userId == nil) {
//        UIAlertController * ac = [UIAlertController alertControllerWithTitle:@"请登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            nil;
//        }];
//        [ac addAction:action];
//        
//        [self presentViewController:ac animated:YES completion:^{
//            nil;
//        }];
//        
//    }
//    else {
//        self.effectView = [[UIView alloc]init];
//        self.effectView.frame = CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height);
//        self.effectView.alpha = 0.3;
//        [self.view addSubview:self.effectView];
//        self.effectView.backgroundColor = [UIColor blackColor];
//        
//        self.openDoorView = [[UITableView alloc] initWithFrame:CGRectMake(30*WIDTH, Main_Screen_Height, Main_Screen_Width-2*30*WIDTH, HEIGTH*450)];
//        self.openDoorView.dataSource = self;
//        self.openDoorView.delegate = self;
//        _openDoorView.backgroundColor = [UIColor whiteColor];
//
//        [self.view addSubview:self.openDoorView];
//        
//        [UIView animateWithDuration:0.5 animations:^{
//            self.effectView.alpha = 0.5;
//            self.openDoorView.rowHeight = 60*HEIGTH;
//            self.openDoorView.separatorStyle = UITableViewCellSeparatorStyleNone;
//            self.openDoorView.frame = CGRectMake(30*WIDTH, Main_Screen_Height-450*HEIGTH, Main_Screen_Width-2*30*WIDTH, HEIGTH*450);
//        }];
//        UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(Main_Screen_Width-4*30*WIDTH, 0, WIDTH*60, HEIGTH*30)];
//        [backBtn setTitle:@"返回" forState:UIControlStateNormal];
//        [backBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//        [backBtn addTarget:self action:@selector(backClicked) forControlEvents:UIControlEventTouchUpInside];
//        [self.openDoorView addSubview:backBtn];
//        [self.openDoorView reloadData];
//    }
//}

////弹出来的界面上添加的返回button的方法
//-(void)backClicked{
//    [UIView animateWithDuration:0.5 animations:^{
//        self.openDoorView.frame = CGRectMake(30*WIDTH, Main_Screen_Height, Main_Screen_Width-60*WIDTH, HEIGTH*450);
//        self.effectView.alpha = 0;
//    } completion:^(BOOL finished) {
//        [self.effectView removeFromSuperview];
//        [self.openDoorView removeFromSuperview];
//    }];
//}

#pragma mark - myTabBar的代理方法
-(void)tabBarView:(id)tabBarView changedIndex:(NSInteger)index{
    self.selectedIndex = index - 1;
}

#pragma mark - tableViewDelegate

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return _openDoorArray.count;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    static NSString * cellStr = @"cell";
//    OpenDoorCell * openCell = [tableView dequeueReusableCellWithIdentifier:cellStr];
//    if (!openCell) {
//        openCell = [[OpenDoorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
//        
//        openCell.backgroundColor = [UIColor clearColor];
//    }
//    
//    NSDictionary * dict = [_openDoorArray objectAtIndex:indexPath.row];
//    
//    openCell.addressLabel.text = dict[@"cname"];
//    
//    return openCell;
//}
//
////开门的功能在这里袄
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSDictionary * dict = [_openDoorArray objectAtIndex:indexPath.row];
//    NSString * urlStr = [NSString stringWithFormat:@"%@%@&menjin_id=%@&user_id=%@",PAOTUIZAI,KAIMEN,dict[@"id"],_userId];
//#pragma mark 数据
//    NSLog(@"___ZDTabBarController___%@", urlStr);
//    [self startAnimation];
//    
//    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
//    
//    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"text/json",@"text/html"]];
//    
//    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
//        NSLog(@"成功");
//    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
//        NSLog(@"%@",[error localizedDescription]);
//    }];
//
//}
//
////开门动画
//- (void) startAnimation
//{
//    UIImageView * View = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width,Main_Screen_Height)];
//    _imageView = View;
//
//    NSMutableArray * imagesArray = [[NSMutableArray alloc] init];
//    for (int i=1; i<17; i++) {
//        NSString * imageName = [NSString stringWithFormat:@"%d",i];
//        UIImage * img = [UIImage imageNamed:imageName];
//        [imagesArray addObject:img];
//    }
//    self.imageView.animationImages = imagesArray;
//    
//    self.imageView.animationDuration = 1.6;
//    
//    self.imageView.animationRepeatCount = 1;
//    
//    [self.view addSubview:self.imageView];
//    
//    [self.imageView startAnimating];
//    
//    [self backClicked];
//}



@end
